package com.example.a1.myapplication.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.a1.myapplication.R;
import com.example.a1.myapplication.adapter.SearchAdapter;
import com.example.a1.myapplication.constants.ConvertConstants;
import com.example.a1.myapplication.constants.TimeConstants;
import com.example.a1.myapplication.utils.ConvertUtils;
import com.example.a1.myapplication.utils.RegexUtils;
import com.example.a1.myapplication.utils.TimeUtils;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;

import java.util.ArrayList;
import java.util.List;

import app.socketlib.com.library.ContentServiceHelper;
import app.socketlib.com.library.listener.SocketResponseListener;
import app.socketlib.com.library.socket.SessionManager;
import app.socketlib.com.library.socket.SocketConfig;

public class TestActivity extends AppCompatActivity {
    private TextView tvText;
    private EditText etText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_convert);
        tvText = findViewById(R.id.tvText);
        etText = findViewById(R.id.etText);
//        initLog();
//        initSocket();
        etText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    showPopupWindow(TestActivity.this, etText);
                    Log.e("TAG", "ACTION_UP");
                }
                return false;
            }
        });
    }

    private void initSocket() {
        SocketConfig socketConfig = new SocketConfig.Builder(getApplicationContext())
                .setIp("192.168.3.102")//ip
                .setPort(60000)//端口
                .setReadBufferSize(10240)//readBuffer
                .setIdleTimeOut(30)//客户端空闲时间,客户端在超过此时间内不向服务器发送数据,则视为idle状态,则进入心跳状态
                .setTimeOutCheckInterval(10)//客户端连接超时时间,超过此时间则视为连接超时
                .setRequestInterval(10)//请求超时间隔时间
                .setHeartbeatRequest("(1,1)\r\n")//与服务端约定的发送过去的心跳包
                .setHeartbeatResponse("(10,10)\r\n") //与服务端约定的接收到的心跳包
                .builder();
        ContentServiceHelper.bindService(this, socketConfig);
        SessionManager.getInstance().setReceivedResponseListener(new SocketResponseListener() {
            @Override
            public void socketMessageReceived(String s) {
                Log.e("TAG", "socketMessageReceived-->" + s);
            }
        });
        SessionManager.getInstance().writeToServer("123");
    }

    private void showPopupWindow(Context context, View view) {
        // 一个自定义的布局，作为显示的内容
        final View contentView = LayoutInflater.from(context).inflate(R.layout.popup_search, null);
        final PopupWindow popupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);

        RecyclerView recyclerView = contentView.findViewById(R.id.rvRV);
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
        layoutManager.setFlexWrap(FlexWrap.WRAP); //设置是否换行
        layoutManager.setFlexDirection(FlexDirection.ROW); // 设置主轴排列方式
        layoutManager.setAlignItems(AlignItems.STRETCH);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        recyclerView.setLayoutManager(layoutManager);
        SearchAdapter adapter = new SearchAdapter(context);
        recyclerView.setAdapter(adapter);
        List<String> list = new ArrayList<>();
        list.add("采购订单1");
        list.add("采购订单2");
        list.add("采购订单3");
        list.add("采购订单4");
        list.add("采购订单5");
        list.add("采购订单6");
        list.add("采购订单7");
        adapter.addList(list);
        popupWindow.showAsDropDown(view, 0, -100);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // popupWindow隐藏时恢复屏幕正常透明度
                setBackgroundAlpha(1.0f);
            }
        });
        setBackgroundAlpha(0.5f);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha 屏幕透明度0.0-1.0 1表示完全不透明
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);

    }

    private void initLog() {
        //ConvertUtils
//        Log.e("TAG", ConvertUtils.bytes2Bits(null));
        Log.e("TAG", "bytes2Bits-->" + ConvertUtils.bytes2Bits(new byte[]{(byte) 0x31, (byte) 0x32, (byte) 0x33}));
        Log.e("TAG", "bits2Bytes-->" + new String(ConvertUtils.bits2Bytes("  ")));
        Log.e("TAG", "bits2Bytes-->" + new String(ConvertUtils.bits2Bytes("001100010011001000110011")));
        Log.e("TAG", "bytes2Chars-->" + new String(ConvertUtils.bytes2Chars(new byte[]{(byte) 0x31, (byte) 0x32, (byte) 0x33})));
        Log.e("TAG", "chars2Bytes-->" + new String(ConvertUtils.chars2Bytes(new char[]{'1', '2', '3'})));
        Log.e("TAG", "bytes2HexString-->" + ConvertUtils.bytes2HexString("123".getBytes()));
        Log.e("TAG", "hexString2Bytes-->" + new String(ConvertUtils.hexString2Bytes("313233")));
        Log.e("TAG", "memorySize2Byte-->" + ConvertUtils.memorySize2Byte(10, ConvertConstants.MB) + "");
        Log.e("TAG", "byte2MemorySize-->" + ConvertUtils.byte2MemorySize(10000, ConvertConstants.KB) + "");
        Log.e("TAG", "byte2FitMemorySize-->" + ConvertUtils.byte2FitMemorySize(10000) + "");

        //RegexUtils
        Log.e("TAG", "isMobileSimple-->" + RegexUtils.isMobileSimple("18642690025") + "");
        Log.e("TAG", "isMobileExact-->" + RegexUtils.isMobileExact("18642690025") + "");
        Log.e("TAG", "isTel-->" + RegexUtils.isTel("18642690025") + "");
        Log.e("TAG", "isUsername-->" + RegexUtils.isUsername("郭郭郭郭郭郭") + "");
        Log.e("TAG", "isIP-->" + RegexUtils.isIP("郭郭郭郭郭郭") + "");
//        Log.e("TAG", "isIP-->" + RegexUtils.getMatches(RegexConstants.REGEX_MOBILE_SIMPLE,"131aaaaaaaa"));
        //TimeUtils
        Log.e("TAG", "getFriendlyTimeSpanByNow-->" + TimeUtils.getFriendlyTimeSpanByNow(getApplicationContext(), System.currentTimeMillis() - TimeConstants.MSEC) + "");
        Log.e("TAG", "getFriendlyTimeSpanByNow-->" + TimeUtils.getFriendlyTimeSpanByNow(getApplicationContext(), System.currentTimeMillis() - TimeConstants.SEC) + "");
        Log.e("TAG", "getFriendlyTimeSpanByNow-->" + TimeUtils.getFriendlyTimeSpanByNow(getApplicationContext(), System.currentTimeMillis() - TimeConstants.MIN) + "");
        Log.e("TAG", "getFriendlyTimeSpanByNow-->" + TimeUtils.getFriendlyTimeSpanByNow(getApplicationContext(), System.currentTimeMillis() - TimeConstants.HOUR) + "");
        Log.e("TAG", "getFriendlyTimeSpanByNow-->" + TimeUtils.getFriendlyTimeSpanByNow(getApplicationContext(), System.currentTimeMillis() - TimeConstants.DAY) + "");
//        Log.e("TAG", "isIP-->" + RegexUtils.getMatches(RegexConstants.REGEX_MOBILE_SIMPLE,"131aaaaaaaa"));
    }
}
